<?php

namespace App\Model;


use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class Support extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'support';

    public function getList(Request $request)
    {
        $where = $request->input('id') ?? 1;
        $sql = "select  a.*,b.id as t_id ,b.type,b.support_name,b.support_desc,b.created_at as t_created_at from cms_support a left join cms_support_type b on a.support_id = b.id where support_id = $where";
        return DB::select($sql);
    }

    public function createSupport(Request $request)
    {
        $data['support_id'] = $request->input('support_id');
        if (!$data['support_id']) {
            return false;
        }
        $supportType = new SupportType();
        $supportTypeInfo = $supportType->getSupportTypeFirst($data['support_id']);

        $data['support_ratio'] = $request->input('support_ratio');
        $data['start_value'] = $request->input('start_value');
        $data['end_value'] = $request->input('end_value');
        $data['is_all'] = $request->input('is_all') ?? 0;
        if ($data['end_value'] == 0) {
            $data['is_all'] = 1;
        } else {
            $data['is_all'] = 0;
            if ($data['end_value'] < $data['start_value']) {
                return false;
            }
        }
        if ($supportTypeInfo->type == 0) {
            $data['is_all'] = 0;
            $data['start_value'] = $data['end_value'] = 0;
        }
        if ($request->input('id')) {
            $where['id'] = $request->input('id');
            return DB::table($this->table)->where($where)->update($data);
        } else {
            return DB::table($this->table)->insertGetId($data);
        }
    }

    public function del($id)
    {
        return DB::table($this->table)->where(['id' => $id])->delete();
    }

    public function delByTypeId($id)
    {
        return DB::table($this->table)->where(['support_id' => $id])->delete();
    }


    public function getSupportRatioValue($support_id = 1, $wealth)
    {
        $support_arr = DB::select("select a.*,b.type from cms_support a left join cms_support_type b on a.support_id = b.id where support_id = $support_id");
        $support_ratio = 0;
        foreach ($support_arr as $v) {
            if ($v->type == 0) {
                //固定扶持
                return $v->support_ratio;
            }
            if ($v->start_value <= $wealth && $v->end_value >= $wealth) {
                return $v->support_ratio;
            }
            if ($v->is_all == 1 && $v->start_value <= $wealth) {
                return $v->support_ratio;
            }
        }
        return $support_ratio;
    }

    public function getSupportIds($id)
    {
        $where['support_id'] = $id;
        return DB::table($this->table)->where($where)->select('id')->get()->toArray();
    }

    public function getRoomSupport($room_id)
    {
        $where['room_id'] = $room_id;
        $res = DB::table('support_room')->where($where)->first();
        return $this->objToArray($res);
    }
}
